package cn.edu.ysu.zry.network;

import cn.hutool.json.JSONObject;

/**
 * @program: XieyiBlockChainOS
 * @description: 区块链系统中相互通讯用的类 T要求是javabean 或者json对象
 * @author: Ruiyuan Zhang
 * @create: 2020-04-20 21:00
 */
public class MessagePack<T> {

    /**
     * message 的取值
     */
    public static class MESSAGE_ZHONGXIN{
        public static final String  GET_ZHONG_LIST="getZhongList";
        public static final String  LINK_TO="linkTo";
        public static final String  GET_PUBLIC_KEY="getPublicKey";
    };
    public static class MESSAGE_ZHONG{
        public static final String VERIFY_XIEYI="verifyXieyi";
        public static final String GET_BLOCK_CHAIN_HEAD="getBlockChainHead";
        public static final String GET_BLOCK_CHAIN="getBlockChain";
        public static final String PUT_XIEYI="putXieyi";
        public static final String PUT_BLOCK="putBlock";
    };

    // 请求方的公钥
    private byte publicKey[];
    private String message;
    private T data;

    /**
    *@Description: 获得message对象的字节码数据
    *@Param:
    *@return:
    *@Author: Ruiyuan Zhang
    *@date: 2020/4/21
    */
    public byte[] getBytes(){
        try {
            JSONObject jsonObject = new JSONObject(this);
            String data = jsonObject.toString();
            return data.getBytes(NetWorkThreadZhongxin.CHARACTER_ENCODING);
        }catch (Exception e){
            return null;
        }
    }




    public byte[] getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(byte[] publicKey) {
        this.publicKey = publicKey;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
